package youleche.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import youleche.com.connection.JdbcHelper;
import youleche.com.idao.ISeller;
import youleche.com.model.MSeller;

/**
 * 商家数据处理
 * @author Administrator
 *
 */
public class DSeller implements ISeller{
	/**
	 * 添加商家信息
	 * @param con 数据库连接
	 * @param seller 商家信息
	 * @return 返回添加影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int addSellerInfo(Connection con, MSeller seller) throws Exception {
		PreparedStatement pre = null;
		try
		{
			String sql = "INSERT seller(userId,shopName,shopAddress,shopImgUrl,isDelete,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15,A16,A17,A18,A19,A20) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
			pre = con.prepareStatement(sql);
			pre.setObject(1, seller.getUserId());
			pre.setObject(2, seller.getShopName());
			pre.setObject(3, seller.getShopAddress());
			pre.setObject(4, seller.getShopImgUrl());
			pre.setObject(5, seller.getIsDelete());
			pre.setObject(6, seller.getA1());
			pre.setObject(7, seller.getA2());
			pre.setObject(8, seller.getA3());
			pre.setObject(9, seller.getA4());
			pre.setObject(10, seller.getA5());
			pre.setObject(11, seller.getA6());
			pre.setObject(12, seller.getA7());
			pre.setObject(13, seller.getA8());
			pre.setObject(14, seller.getA9());
			pre.setObject(15, seller.getA10());
			pre.setObject(16, seller.getA11());
			pre.setObject(17, seller.getA12());
			pre.setObject(18, seller.getA13());
			pre.setObject(19, seller.getA14());
			pre.setObject(20, seller.getA15());
			pre.setObject(21, seller.getA16());
			pre.setObject(22, seller.getA17());
			pre.setObject(23, seller.getA18());
			pre.setObject(24, seller.getA19());
			pre.setObject(25, seller.getA20());
	
			return pre.executeUpdate();
		}finally{
			JdbcHelper.free(null, pre);
		}
	}

	/**
	 * 根据用户Id获取商家信息
	 * @param con 数据库连接
	 * @param userId 用户ID
	 * @return 返回商家信息
	 * @throws Exception 异常
	 */
	@Override
	public MSeller getSellerByUserId(Connection con, String userId)
			throws Exception {
		PreparedStatement pre = null;
		ResultSet rs = null;
		try{
			
			String sql = "SELECT userId,shopName,shopAddress,shopImgUrl,isDelete," +
					"A1,A2,A3,A4,A5," +
					"A6,A7,A8,A9,A10," +
					"A11,A12,A13,A14,A15," +
					"A16,A17,A18,A19,A20 " +
					"FROM seller " +
					"where userId='"+userId+"' and isDelete='0'";
			pre = con.prepareStatement(sql);
			rs = pre.executeQuery();
			MSeller seller = null;
			if(rs != null){
				while(rs.next()){
					seller = new MSeller();
					seller.setUserId(rs.getString(1));
					seller.setShopName(rs.getString(2));
					seller.setShopAddress(rs.getString(3));
					seller.setShopImgUrl(rs.getString(4));
					seller.setIsDelete(rs.getString(5));
					seller.setA1(rs.getString(6));
					seller.setA2(rs.getString(7));
					seller.setA3(rs.getString(8));
					seller.setA4(rs.getString(9));
					seller.setA5(rs.getString(10));
					seller.setA6(rs.getString(11));
					seller.setA7(rs.getString(12));
					seller.setA8(rs.getString(13));
					seller.setA9(rs.getString(14));
					seller.setA10(rs.getString(15));
					seller.setA11(rs.getString(16));
					seller.setA12(rs.getString(17));
					seller.setA13(rs.getString(18));
					seller.setA14(rs.getString(19));
					seller.setA15(rs.getString(20));
					seller.setA16(rs.getString(21));
					seller.setA17(rs.getString(22));
					seller.setA18(rs.getString(23));
					seller.setA19(rs.getString(24));
					seller.setA20(rs.getString(25));
					
					break;
				}
			}
			
			return seller;
		}finally{
			JdbcHelper.free(rs, pre);
		}
	}

	/**
	 * 更新商家信息
	 * @param con 数据库连接
	 * @param seller 商家信息
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int updateSellerInfoById(Connection con, MSeller seller)
			throws Exception {
		PreparedStatement ps = null;
		try
		{
			String sql = "update seller set shopName='"+seller.getShopName()+"', shopAddress='"+seller.getShopAddress()+"' where userId='"+seller.getUserId()+"' and isDelete='0' limit 1;";
			 ps = con.prepareStatement(sql);
			return ps.executeUpdate();
		}finally{
			JdbcHelper.free(null, ps);
		}
	}

	/**
	 * 更新商家认证图片地址
	 * @param con 数据库连接
	 * @param seller 商家信息
	 * @return 返回更新影响的行数
	 * @throws Exception 异常
	 */
	@Override
	public int updateSellerImageUrlById(Connection con, MSeller seller)
			throws Exception {
		PreparedStatement ps = null;
		try
		{
			String sql = "update seller set shopImgUrl='"+seller.getShopImgUrl()+"' where userid='"+seller.getUserId()+"' and isdelete='0' limit 1;";
			ps = con.prepareStatement(sql);
			
			return ps.executeUpdate();
		}finally{
			JdbcHelper.free(null, ps);
		}
	}

	/**
	 * 得到商家的信用指数
	 * @param con 数据库连接
	 * @param sellserId 商家ID
	 * @return 返回信用指数
	 * @throws Exception
	 */
	@Override
	public String getSellerCreditById(Connection con, String sellserId)
			throws Exception {
		PreparedStatement pre = null;
		ResultSet rs = null;
		try{
			
			String sql = "SELECT " +
					"A1 " +					
					"FROM seller " +
					"where userId='"+sellserId+"' and isDelete='0'";
			pre = con.prepareStatement(sql);
			rs = pre.executeQuery();
			if(rs != null){
				while(rs.next()){
					return rs.getString(1);
				}
			}
			
			return "0";
		}finally{
			JdbcHelper.free(rs, pre);
		}
	}

	/**
	 * 更新商家信用指数
	 * @param con 数据库连接
	 * @param sellerId 商家Id
	 * @return 返回更新影响的行数
	 * @throws Exception
	 */
	@Override
	public int updateSellerCreditById(Connection con, String sellerId, String creditIndex)
			throws Exception {
		PreparedStatement ps = null;
		try
		{
			String sql = "update seller set A1='"+creditIndex+"' where userid='"+sellerId+"' and isdelete='0' limit 1;";
			ps = con.prepareStatement(sql);
			
			return ps.executeUpdate();
		}finally{
			JdbcHelper.free(null, ps);
		}
	}

}
